package it.saga.pes.frontoffice.servizi.struts2.upload;

import it.saga.pes.frontoffice.servizi.struts2.serviziostep.Allegato;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings({ "serial", "unchecked" })
@Namespace("/opencms/system/modules/it.saga.pes.frontoffice.servizi.struts2.serviziostep/services/upload")
@InterceptorRefs({ @InterceptorRef(value = "fileUpload", params = { "maximumSize", "1048576" }), // 1M
		@InterceptorRef(value = "basicStack") })
public class UploadAction extends ActionSupport implements ServletRequestAware, SessionAware {

	private static String LISTA_ALLEGATI = "LISTA_ALLEGATI";

	private List<Allegato> allegati = null;
	private File fileAllegato;
	private String fileAllegatoFileName;
	private String fileAllegatoContentType;
	private HttpServletRequest servletRequest;

	private String backStep;
	private String nextStep;
	private String curStep;

	private Map<String, Object> session;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@SkipValidation
	@Action(value = "UploadList", results = { @Result(name = "success", location = "/upload/UploadList.jsp") })
	public String init() {
		System.out.println("backStep:" + backStep);
		System.out.println("nextStep:" + nextStep);
		allegati = (List<Allegato>) session.get(LISTA_ALLEGATI);
		if (allegati == null)
			allegati = new ArrayList<Allegato>();
		session.put(LISTA_ALLEGATI, allegati);
		return SUCCESS;
	}

	@Action(value = "add", results = { @Result(name = "success", location = "/upload/UploadList.jsp"),
			@Result(name = "input", location = "/upload/UploadList.jsp") })
	public String add() {
		allegati = (List<Allegato>) session.get(LISTA_ALLEGATI);
		try {
			String filePath = servletRequest.getSession().getServletContext().getRealPath("/upload");
			System.out.println("Server path:" + filePath);
			System.out.println("redirectUrl:" + backStep);
			System.out.println("nextUrl:" + nextStep);
			File fileToCreate = new File(filePath, this.fileAllegatoFileName);
			FileUtils.copyFile(this.fileAllegato, fileToCreate);
			Allegato a = new Allegato();
			a.setFilename(fileAllegatoFileName);
			a.setUrl(fileToCreate.getPath());
			a.setTimestamp(new Date());
			allegati.add(a);
			session.put(LISTA_ALLEGATI, allegati);
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	@Action(value = "remove", results = { @Result(name = "success", location = "/upload/UploadFile.jsp"), })
	public String remove() {
		return SUCCESS;
	}

	/** METODI STEP **/
	@Action(value = "next", results = { @Result(name = "next", location = "${nextStep}", type = "redirect") })
	public String next() {
		int step = (Integer) session.get("STEP");
		step++;
		session.put("STEP", step);
		return "next";
	}

	@Action(value = "back", results = { @Result(name = "back", location = "${backStep}", type = "redirect") })
	public String back() {
		int step = (Integer) session.get("STEP");
		step--;
		session.put("STEP", step);
		return "back";
	}

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public List<Allegato> getAllegati() {
		return allegati;
	}

	public void setAllegati(List<Allegato> allegati) {
		this.allegati = allegati;
	}

	public File getFileAllegato() {
		return fileAllegato;
	}

	public void setFileAllegato(File fileAllegato) {
		this.fileAllegato = fileAllegato;
	}

	public String getFileAllegatoFileName() {
		return fileAllegatoFileName;
	}

	public void setFileAllegatoFileName(String fileAllegatoFileName) {
		this.fileAllegatoFileName = fileAllegatoFileName;
	}

	public String getFileAllegatoContentType() {
		return fileAllegatoContentType;
	}

	public void setFileAllegatoContentType(String fileAllegatoContentType) {
		this.fileAllegatoContentType = fileAllegatoContentType;
	}

	public String getBackStep() {
		return backStep;
	}

	public void setBackStep(String backStep) {
		this.backStep = backStep;
	}

	public String getNextStep() {
		return nextStep;
	}

	public void setNextStep(String nextStep) {
		this.nextStep = nextStep;
	}

	public String getCurStep() {
		return curStep;
	}

	public void setCurStep(String curStep) {
		this.curStep = curStep;
	}

}
